package com.xing.hospital.common.utils;

import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;

/**
 * MD5加密工具类
 * encrypt 加密
 *
 * @Author: xingfuhao
 * @Date: 2022/05/11/14:18
 */
public final class MD5 {

    public static void main(String[] args) {
        String encrypt = encrypt("123456");
        System.out.printf("MD5加密："+encrypt);
        //e10adc3949ba59abbe56e057f20f883e

    }

    public static String encrypt(String strSrc) {

        try {
            //十六进制字符串
            char hexChars[] = {'0', '1', '2', '3', '4', '5', '6', '7', '8',
                    '9', 'a', 'b', 'c', 'd', 'e', 'f'};
            //得到字节
            byte[] bytes = strSrc.getBytes();
            //消息摘要 得到实例
            MessageDigest md = MessageDigest.getInstance("MD5");
            //更新字节
            md.update(bytes);
            //消化
            bytes = md.digest();
            int j = bytes.length;
            char[] chars = new char[j * 2];
            int k = 0;
            for (int i = 0; i < bytes.length; i++) {
                byte b = bytes[i];
                chars[k++] = hexChars[b >>> 4 & 0xf];
                chars[k++] = hexChars[b & 0xf];
            }
            return new String(chars);
        } catch (NoSuchAlgorithmException e) {
            e.printStackTrace();
            throw new RuntimeException("MD5加密出错！！+" + e);
        }

    }
}
